#include "BDSender.h"
#include <iostream>

BDSender::BDSender(std::string address)
	: m_zmq(address)
{
	m_zmq.Init();
}


BDSender::~BDSender()
{
	m_zmq.DeInit();
}

void BDSender::SendMsg(MessageType type, google::protobuf::Message* message)
{
	unsigned int name_len = message->GetDescriptor()->name().size();
	const char* name = message->GetDescriptor()->name().c_str();
	unsigned int type_val = static_cast<char>(type);
	int message_size = message->ByteSize();
	unsigned int totoal_size = sizeof(unsigned int) + name_len + sizeof(unsigned int) + message_size;
	char* buffer = new char[totoal_size];
	char* current = buffer;
	
	memcpy(current, &name_len, sizeof(unsigned int));
	current += sizeof(unsigned int);
	memcpy(current, name, name_len);
	current += name_len;
	memcpy(current, &type_val, sizeof(unsigned int));
	current += sizeof(unsigned int);
	message->SerializeToArray(current, message_size);
	delete message;
	message = NULL;
	m_zmq.SendMsg(buffer, totoal_size);
}